//
//  TenDetailViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/3/30.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

enum TenGestureType:Int {
    case TenGestureType_Tap = 0
    case TenGestureType_Swipe
    case TenGestureType_Pan
    case TenGestureType_Rotation
    case TenGestureType_Pinch
    case TenGestureType_LongPress
}

class TenDetailViewController: UIViewController {

    var gestureType = TenGestureType.TenGestureType_Tap;
    
    var bgView:TenView?;
    fileprivate var lastRotationRadians:CGFloat = 0;
    fileprivate var lastPinchScale:CGFloat = 0;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = self.onShowTitle(type: gestureType);
        // Do any additional setup after loading the view.
        self.view.backgroundColor = .white;
        self.onCreateUIForView();
        
        self.onAddGestureAction();
    }
    
    //TODO:UI
    fileprivate func onCreateUIForView() {
        self.bgView = TenView(frame: CGRect(x: (screenMainSize.width-60)/2, y: (screenMainSize.height-60)/2, width: 60, height: 60));
        self.bgView?.backgroundColor = .clear;
        self.view.addSubview(self.bgView!);
    }

    //TODO:Actions
    fileprivate func onShowTitle(type:TenGestureType) -> String {
        var typeName = "点按";
        if type == TenGestureType.TenGestureType_Tap {
            typeName = "点按";
        }
        else if type == TenGestureType.TenGestureType_Pan {
            typeName = "拖动";
        }
        else if type == TenGestureType.TenGestureType_Swipe {
            typeName = "轻扫";
        }
        else if type == TenGestureType.TenGestureType_Rotation {
            typeName = "旋转";
        }
        else if type == TenGestureType.TenGestureType_Pinch {
            typeName = "捏合";
        }
        else if type == TenGestureType.TenGestureType_LongPress {
            typeName = "长按";
        }
        return typeName;
    }
    
    fileprivate func onAddGestureAction(){
        switch self.gestureType {
        case .TenGestureType_Tap:
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onShowGestureForTap));
            self.bgView?.addGestureRecognizer(tapGesture);
            break
        case .TenGestureType_Swipe:
            //轻扫 一个手势支持一个方向
            let swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: #selector(onShowGestureForSwipe(swipe:)));
            swipeLeftGesture.direction = .left;
            self.view?.addGestureRecognizer(swipeLeftGesture);
            
            let swipeRightGesture = UISwipeGestureRecognizer(target: self, action: #selector(onShowGestureForSwipe(swipe:)));
            swipeRightGesture.direction = .right;
            self.view?.addGestureRecognizer(swipeRightGesture);
            break
        case .TenGestureType_Pan:
            let pan = UIPanGestureRecognizer(target: self, action: #selector(onShowGestureForPan(pan:)));
            self.bgView?.addGestureRecognizer(pan);
            break
        case .TenGestureType_Rotation:
            let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(onShowGestureForRotation(rotation:)));
            self.bgView?.addGestureRecognizer(rotationGesture);
            break
        case .TenGestureType_Pinch:
            let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(onShowGestureForPinch(_pinch:)));
            self.bgView?.addGestureRecognizer(pinchGesture);
            break;
        case .TenGestureType_LongPress:
            let longPress = UILongPressGestureRecognizer(target: self, action: #selector(onShowGestureForLongPress(_longPress:)));
            longPress.minimumPressDuration = 3;
            self.bgView?.addGestureRecognizer(longPress);
            break
        default:
            ""
        }
    }
    
    //TODO:点击
    @objc fileprivate func onShowGestureForTap() {
        let fillColor = UIColor(red: CGFloat(arc4random()%255)/255.0, green: CGFloat(arc4random()%255)/255.0, blue: CGFloat(arc4random()%255)/255.0, alpha: 1.0);
        let strockColor = UIColor(red: CGFloat(arc4random()%255)/255.0, green: CGFloat(arc4random()%255)/255.0, blue: CGFloat(arc4random()%255)/255.0, alpha: 1.0);
        self.bgView?.onChangeUIForTap(fill: fillColor, strock: strockColor);
    }

    //TODO:轻扫
    @objc fileprivate func onShowGestureForSwipe(swipe:UISwipeGestureRecognizer) {
        let frame = self.bgView?.frame;
        if swipe.direction == .left {
            UIView.animate(withDuration: 0.35) {
                self.bgView?.frame = CGRect(x: (screenMainSize.width-frame!.width-10)/2, y: (screenMainSize.height-frame!.height-10)/2, width: frame!.width+10, height: frame!.height+10);
            };
            self.onShowGestureForTap();
        }
        else if swipe.direction == .right {
            UIView.animate(withDuration: 0.35) {
                self.bgView?.frame = CGRect(x: (screenMainSize.width-frame!.width+10)/2, y: (screenMainSize.height-frame!.height+10)/2, width: frame!.width-10, height: frame!.height-10);
            };
            self.onShowGestureForTap();
        }
    }
    
    //TODO:拖动
    @objc fileprivate func onShowGestureForPan(pan:UIPanGestureRecognizer) {
        let point = pan.location(in: self.view);
        let size = (self.bgView?.frame.size)!;
        self.bgView?.frame = CGRect(origin: CGPoint(x: point.x-size.width/2, y: point.y-size.height/2), size:size );
    }
    
    //TODO:旋转
    @objc fileprivate func onShowGestureForRotation(rotation:UIRotationGestureRecognizer) {
        let value = rotation.rotation;
        //上一次角度+本次旋转的角度
        self.bgView?.transform = CGAffineTransform(rotationAngle: self.lastRotationRadians+value);

        if rotation.state == .ended {
            self.lastRotationRadians += value;
        }
    }
    
    //TODO:捏合
    @objc fileprivate func onShowGestureForPinch(_pinch:UIPinchGestureRecognizer) {
        print("\(_pinch.scale)");
        
        if _pinch.state == .ended {
            self.lastPinchScale = 0;
            self.bgView?.transform = CGAffineTransform.identity;
            return;
        }
        
        let newScale:CGFloat = _pinch.scale;
        
        self.bgView?.transform = (self.bgView?.transform.scaledBy(x: newScale, y: newScale))!;
        
        self.lastPinchScale = _pinch.scale;
    }
    
    //TODO:长按
    @objc fileprivate func onShowGestureForLongPress(_longPress:UILongPressGestureRecognizer) {
        self.onShowGestureForTap();
    }
}
